<template>
  <div class="areaContainer">
    <div class="search">
      <el-input v-model.trim="searchValue" placeholder="请输入学校名称关键字进行查询" clearable :prefix-icon="Search" @keyup="getUniverList(searchValue)">
        <template #append>
          <el-button :icon="Search"  @click="getUniverList(searchValue)"/>
        </template>
      </el-input>
      <el-button style="margin-left: 30px;" type="primary" @click="addCollegeVisable = true">添加学院</el-button>
      <el-dialog v-model="addCollegeVisable" title="添加学院" width="30%" draggable>
        <el-form v-model="addCollegeForm" label-position="right">
          <el-form-item prop="collegeName"  width="100" label="学院名称">
            <el-input v-model="addCollegeForm.collegeName" placeholder="请输入学院名称"  width="150"/>
          </el-form-item>
          <el-form-item prop="univerId"  width="100" label="学校ID">
            <el-input v-model="addCollegeForm.univerId" placeholder="请输入学校ID"  width="200"/>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="addCollegeVisable = false">取消</el-button>
            <el-button type="primary" @click="addCollege()">确认</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
    <el-table :data="collegeData" stripe style="width: 100%;margin-top: 10px;" height="50%"  :default-sort="{ prop: 'createTime', order: 'descending' }">
    <el-table-column prop="cdId" label="学院详情ID" width="200"/>
    <el-table-column prop="collegeId" label="学院ID" width="200"/>
    <el-table-column prop="detail" label="详情内容" width="200"/>
    <el-table-column prop="createTime" label="创建时间" width="250" sortable>
      <template #default="row">
        <div>{{timeTuTIL.myTimeFormat(row.row.createTime)}}</div>
      </template>
    </el-table-column>     
    <el-table-column fixed="right" label="操作" width="300">
      <template #default="row">
          <el-tooltip content="编辑" placement="bottom">
            <el-button link type="primary" :icon="Edit"  class="myLink" @click="clickEditHandler(row)" />
          </el-tooltip>
          <el-tooltip content="删除" placement="bottom">
            <el-button link type="danger" :icon="Delete"  class="myLink" @click="collegeDelete(row)"></el-button>
          </el-tooltip>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="editCollegeVisible" title="编辑" draggable>
    <el-form :model="editCollegeForm" label-width="80px">
      <el-form-item label="学院ID" prop="collegeId">
        <el-input v-model="editCollegeForm.collegeId" autocomplete="off" disabled/>
      </el-form-item>
      <el-form-item label="学院名称" prop="collegeName">
        <el-input v-model="editCollegeForm.collegeName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="所属学校ID" prop="univerId">
        <el-input v-model="editCollegeForm.univerId" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="editCollegeVisible = false">取消</el-button>
        <el-button type="primary" @click="collegeEdit()">提交修改</el-button>
      </span>
    </template>
  </el-dialog>
   <div class="pagination">
    <el-pagination v-model:current-page="page" v-model:page-size="size" :page-sizes="[5,10,20,30]" :small="small" :disabled="disabled"
      :background="background" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
  </div>
  </div>
</template>

<script setup>
import { reactive , inject , onMounted , ref } from "vue"
import axios from '../../axios/axiosInstance'
import timeTuTIL from '../../utils/timeUtilT'
import { ElLoading,ElMessage ,ElMessageBox} from 'element-plus'
import { Delete ,Edit,Search} from '@element-plus/icons-vue'

//变量定义
let editCollegeVisible = ref(false)
let addCollegeVisable = ref(false)
let collegeData = reactive([])

let searchValue = ref()
let editCollegeForm = reactive({
  cdId: '',
  collegeId: '',
  detail: '',
})

let addCollegeForm = reactive({
  cdId: '',
  collegeId: '',
  detail: '',
})
const total = ref(0)
let page = 1
let size = 5
const small = ref(false)
const background = ref(false)
const disabled = ref(false)

//方法 

const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  size = val
  init()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  page = val
  init()
}

 const clear = (value) => {
    Object.keys(value).map(key => {
      delete value[key]
    })
  }

const getUniverList = (searchValue) => {
  if(searchValue != ''){
    axios.get(`/contact-service/college-detail/likeList/${searchValue}`).then((res) => {
        console.log(res)    
        if(res.code == 200){
          clear(collegeData)
          for(let i in res.data){
            console.log(i)
            collegeData.push(res.data[i])
          }
          ElMessage({type: "success",message: "查询成功"});
        }else{
          ElMessage({type: "error",message: "查询失败"});
       }
      }).catch((err) => {
        console.log(err)
      })
  }else{
    init()
  }
}

const addCollege = () =>{
  //向后端发送请求
   axios.post(`/contact-service/college-detail/add/${addUniverForm.collegeId}/${addUniverForm.detail}`).then((res) => {   
      console.log("res为：")
      console.log(res)
      if(res.code == 200){
          //添加停车区域成功
          ElMessage({type: "success",message: "添加成功"});
          collegeData.push(res.data)
        }else{
          //添加失败
         ElMessage({type: "error",message: "添加失败"})
        }
    
    }).catch((err) => {

      console.log(err)

    })

}

const clickEditHandler = (row) => {
  editCollegeVisible.value = true;
  console.log("row为：")
  console.log(row)
  editCollegeForm.cdId = row.row.cdId;
  editCollegeForm.collegeId = row.row.collegeId;
  editCollegeForm.detail = row.row.detail;
  console.log(editCollegeForm)
}

const collegeEdit = () => {
  let cdId = editUniverForm.cdId
  let collegeId = editUniverForm.collegeId
  let detail = editUniverForm.detail
   axios.put(`/contact-service/college-detail/update/${cdId}/${collegeId}/${detail}` ).then((res) => {
    console.log(res)
    if(res.code == 200){
      ElMessage({type: "success",message: "编辑成功"});
      collegeData.pop(row.$index)
    }else{
      ElMessage({type: "error",message: "编辑失败"});
    }
  }).catch((err) => {
    console.log(err)
  })
}

const collegeDelete = (row,index) => {
  console.log("删除的row为：")
  console.log(row)
  let cdId = row.row.cdId
  ElMessageBox.confirm(
    '确定要删除学院详情【'+cdId+'】吗？',
    'Warning',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning',
    }
  )
    .then(() => {
      axios.delete('/contact-service/college-detail/delete/'+cdId).then((res) => {
        console.log(res)
        if(res == true){
          ElMessage({type: "success",message: "删除成功"});
          collegeData.pop(row.$index)
        }else{
          ElMessage({type: "error",message: "删除失败"});
       }
      }).catch((err) => {
        console.log(err)
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '删除取消',
      })
    }) 
}

const init = () => {
  const loading = ElLoading.service({
        lock: true,
        text: '拼命加载中',
          background: 'rgba(0, 0, 0, 0.7)',
      })
      setTimeout(() => {
        loading.close()
      }, 2000)
     axios.get(`/contact-service/college-detail/allList/${page}/${size}`).then((res) => {   
      console.log("res为：")
      console.log(res)
      clear(collegeData)
      total.value = res.data.total
      for(let i in res.data.content){
        console.log(i)
        collegeData.push(res.data.content[i])
      }
      console.log("最后的areaData是：")
      console.log(collegeData)
    }).catch((err) => {

      console.log(err)

    })
}

//页面初始化方法
onMounted(()=>{ 
  init()
})
</script>

<style scoped>
.areaContainer .search{
  display: flex;
  width: 600px;
}
.areaContainer .pagination{
 justify-content: center;
 margin-top: 15px;
}

</style>
